//
//  CinemaViewController.m
//  结构
//
//  Created by 看着太阳吃烧饼 on 15/7/17.
//  Copyright (c) 2015年 看着太阳吃烧饼. All rights reserved.
//

#import "CinemaViewController.h"
#import "DataService.h"
#import "cinemaModal.h"
#import "districtModal.h"
#import "CinemaCell.h"
#define kwidth [UIScreen mainScreen].bounds.size.width
#define kheight [UIScreen mainScreen].bounds.size.height
@interface CinemaViewController ()
{
    BOOL flags[1000];
}
@end

@implementation CinemaViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"观影";
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createCinemaTableView];
    [self _loadData];
//    NSLog(@"%@",_cinemaModalArray);
    // Do any additional setup after loading the view.
}

- (void)_createCinemaTableView {
    self.view.backgroundColor = [UIColor blackColor];
    _cinemaTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    _cinemaTableView.delegate = self;
    _cinemaTableView.dataSource = self;
    [self.view addSubview:_cinemaTableView];
    
    UINib *nib = [UINib nibWithNibName:@"CinemaCell" bundle:[NSBundle mainBundle]];
    [_cinemaTableView registerNib:nib forCellReuseIdentifier:@"cell"];
}

- (void)_loadData {
    //解析电影院的数据
    _cinemaModalArray = [[NSMutableArray alloc] init];
    NSDictionary *cinemaDic = [DataService getJsonDataFromFile:@"cinema_list.json"];
    NSArray *cinemaArray = [cinemaDic objectForKey:@"cinemaList"];
    for (NSDictionary *dic in cinemaArray) {
        cinemaModal *modal = [[cinemaModal alloc] init];
        [modal setValuesForKeysWithDictionary:dic];
        [_cinemaModalArray addObject:modal];
    }
    //解析地区的数据
    _districtModalArray = [[NSMutableArray alloc] init];
    
    NSDictionary *districtDic = [DataService getJsonDataFromFile:@"district_list.json"];
    NSArray *districtArray = [districtDic objectForKey:@"districtList"];
    for (NSDictionary *dic2 in districtArray) {
        districtModal *modal2 = [[districtModal alloc] init];
        [modal2 setValuesForKeysWithDictionary:dic2];
        [_districtModalArray addObject:modal2];
    }
    //构造整体数组
    _dcArray = [[NSMutableArray alloc] init];
    for (int i = 0; i<_districtModalArray.count; i++) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        cinemaModal *modal4 = _districtModalArray[i];
        for (int j = 0; j<_cinemaModalArray.count; j++) {
            cinemaModal *modal3 = _cinemaModalArray[j];
            if ([modal3.districtId isEqualToString:modal4.districtId]) {
                [array addObject:modal3];
            }
//            if ([modal4.districtId isEqualToString:@"2116"]) {
//                NSLog(@"%li",array.count);
//            }
        }
        
//        if ([modal4.districtId isEqualToString:@"1011"]) {
//            NSLog(@"%@",array);
//        }

        [_dcArray addObject:array];
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _districtModalArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    直接获取方法
//    districtModal *modal2 = _districtModalArray[section];
//    int i = 0;
//    int j = 0;
//    while (j<_cinemaModalArray.count) {
//        cinemaModal *modal1 =  _cinemaModalArray[j];
//        if ([ modal1.districtId  isEqualToString:modal2.districtId ]) {
//            i++;
//        }
//        j++;
//    }
//    if (flags[section] == 0) {
//        return 0;
//    }
//
//    return i;
    
    //构造数组获取方法
    NSArray *array = _dcArray[section];
    if (flags[section] == 0) {
        return 0;
   }
    return array.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CinemaCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NSArray *array = _dcArray[indexPath.section];
//    NSLog(@"%li %li",indexPath.section,array.count);
    cinemaModal *modal = array[indexPath.row];
    cell.modal = modal;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIButton *button = [[UIButton alloc] init];
//    [button setBackgroundImage:[UIImage imageNamed:@"poster_title_home.png"] forState:UIControlStateNormal];
//    [button setBackgroundColor:[UIColor redColor]];
    button.tag = section;
    [button addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    districtModal *modal = _districtModalArray[section];
    NSString *name = modal.name;
    [button setBackgroundImage:[UIImage imageNamed:@"hotMovieBottomImage@2x"] forState:UIControlStateNormal];
    [button setTitle:name forState:UIControlStateNormal];
    return button;
}

- (void)btnAction:(UIButton *)button {
    NSInteger index = button.tag;
    
    flags[index] = !flags[index];
    
    [_cinemaTableView reloadData];
    NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
//    NSLog(@"%@",indexSet);
    
    [_cinemaTableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationFade];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
